﻿
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- saved from url=(0014)about:internet -->
<html xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:mssdk="winsdk" xmlns:script="urn:script" xmlns:build="urn:build" xmlns:MSHelp="http://msdn.microsoft.com/mshelp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="Description" content="Executing Until a Specified State is Reached"/>
<meta name="MSHAttr" content="PreferredSiteName:MSDN"/>
<meta name="MSHAttr" content="PreferredLib:/library/windows/hardware"/>
<title>Executing Until a Specified State is Reached</title>

<meta name="MS-HAID" content="t01_basic_e0823c22-0440-4864-bddd-0673589fc88b.xml"/>


<link rel="STYLESHEET" type="text/css" HREF="../common/backsdk4.css"/>





<style>
html,div { margin: 0; padding: 0;}

body {
	padding: 0px;
	margin: 0px;
	overflow: auto;
	height: 100%;
}

#winchm_template_button{
	float: right;
	width: 93px;
	top: 7px;
	position: relative;
	text-align: right;
	right: 5px;
	height: auto;
}

#winchm_template_top{
	padding: 0px;
	margin: 0px;
	border-bottom: 1px solid #9B9B9B;
	background-color: #B1CEFE;
}

#winchm_template_navigation{
	margin: 0px;
	padding-top: 7px;
	padding-left: 7px;
	padding-bottom: 3px;
	padding-right: 0px;
	font-size: 8.5pt;
	font-family: Arial, Helvetica, sans-serif;
	font-weight: normal;
	color: #585858;
}

#winchm_template_title{
	margin: 0px;
	padding-top: 4px;
	padding-left: 7px;
	padding-bottom: 7px;
	padding-right: 0px;
	font-size: 18px; 
	font-family: Verdana, Geneva, sans-serif;
	color: #363636;
}

#winchm_template_content{
	margin-top: 20px;
	margin-left: 15px;
	margin-bottom: 20px;
	margin-right: 15px;
	width: auto  !important;
	width: 100%;
}

#winchm_template_footer{
	border-width: 1px;
	border-color: #B1CEFE;
	border-top-style: solid;
	margin-top: 15px;
	margin-left: 15px;
	margin-bottom: 20px;
	margin-right: 15px;
	padding-top: 7px;
	padding-left: 0px;
	padding-bottom: 0px;
	padding-right: 0px;
	font-family: arial, helvetica, sans-serif;
	font-size: 8.5pt;
	color: #696969;
	width: auto;
	text-align: left;
}


#winchm_template_container{
	margin: 0px;
	padding: 0px;
	position: static;
	padding-bottom: 3px;
	overflow: auto;
	background-color: #FFFFFF;
}


@media print
{
#winchm_template_container{
	position: static;	
	margin: 0px;
	padding: 5px;
	
	width: auto;
	height: auto;
	overflow: auto;
}
#winchm_template_button{
visibility:hidden;
}
}

#winchm_template_navigation A:link	{text-decoration: none; color:#004080}
#winchm_template_navigation A:visited  {text-decoration: none; color: #004080}
#winchm_template_navigation A:active {text-decoration: none; color: #004080 }
#winchm_template_navigation A:hover {text-decoration: none;color: #0080FF}

A:link	{text-decoration: underline; color:#0033CC}
A:visited  {text-decoration: underline; color: #0033CC}
A:active {text-decoration: underline; color: #0033CC }
A:hover {text-decoration: underline;color: #FF0000;}
</style>
<script type="text/javascript">
function isMobile(){
Agent = window.navigator.userAgent;
if (Agent.indexOf("iPhone")>=1 || Agent.indexOf("iPad")>=1 || Agent.indexOf("iPod")>=1 || Agent.indexOf("Android")>=1){
return true;
}else{
return false;	
}

}
function d_onresize(){
if (window.navigator.userAgent.indexOf("MSIE")>=1){
document.getElementById('winchm_template_container').style.pixelWidth = document.body.offsetWidth - 3;
document.getElementById('winchm_template_container').style.pixelHeight = document.body.offsetHeight - document.getElementById('winchm_template_top').offsetHeight - 4;
}
document.getElementById('winchm_template_container').style.top = document.getElementById('winchm_template_top').offsetHeight + 'px';
}

function d_onbeforeprint(){
document.getElementById('winchm_template_container').style.width = 'auto';
document.getElementById('winchm_template_container').style.height = 'auto';
}

function d_onafterprint(){
d_onresize();
}

if(!isMobile()){

window.onload = d_onresize;
window.onresize = d_onresize;
window.onbeforeprint = d_onbeforeprint;
window.onafterprint = d_onafterprint;

document.write("<style>\n");
document.write("body {overflow: hidden;}\n");
document.write("#winchm_template_container {position: absolute;overflow: auto;top : 0px;right: 0px;bottom: 0px;left: 0px;}\n");
document.write("</style>\n");
}

</script>
</head>
<body><script language="JavaScript" type="text/JavaScript">
function syn(){
if(parent.nav.tree){
 if(parent.nav.tree.loaded){
  parent.nav.tree.selectNode(1359);
 }else{
  setTimeout("syn()",500);
}
  }else{
  setTimeout("syn()",500);
  }}
if(parent!=self){
  setTimeout("syn()",100);
}else{
  parent.location.href = "../../index.htm?page=debugger/executing_until_a_specified_state_is_reached.htm";
}
originalOnload = window.onload;
if(originalOnload==null){
window.onload = function(){parent.contentLoaded = true;};
}else{
window.onload = function(){originalOnload();parent.contentLoaded = true;};
}
</script> 


<div id="winchm_template_top">
	<div id="winchm_template_button"><A href="setting_a_conditional_breakpoint.htm" title="Previous topic"><img id="winchm_template_prev" alt="Previous topic" src="../template2/btn_prev_n.gif" border="0"></a><A href="reading_and_writing_memory.htm" title="Next topic"><img id="winchm_template_next" alt="Next topic" src="../template2/btn_next_n.gif" border="0"></a></div>
	<div id="winchm_template_navigation">Help &gt; 
<A href="introduction6.htm">Debugging Tools for Windows (WinDbg, KD, CDB, NTSD)</A> &gt; <A href="debugging_techniques.htm">Debugging Techniques</A> &gt; <A href="standard_debugging_techniques.htm">Standard Debugging Techniques</A> &gt; <A href="using_breakpoints.htm">Using Breakpoints</A> &gt; </div>
	<div id="winchm_template_title">Executing Until a Specified State is Reached</div>
</div>
<div id="winchm_template_container">
	<div id="winchm_template_content"><div id="mainSection"><p>There are several ways to cause the target to execute until a specified state is reached.</p>
<h3><a id="using_a_breakpoint_to_control_execution"></a><a id="USING_A_BREAKPOINT_TO_CONTROL_EXECUTION"></a>Using a Breakpoint to Control Execution</h3>
<p>One method is to use a breakpoint.  The simplest breakpoint halts execution when the program counter reaches a specified address.  A more complex breakpoint can:</p>
<ul>
<li>
<p>be triggered only when this address is executed by a specific thread, </p>
</li>
<li>
<p>allow a specified number of passes through this address before being triggered, </p>
</li>
<li>
<p>automatically issue a specified command when it is triggered, or </p>
</li>
<li>
<p>watch a specified address in non-executable memory, being triggered when that memory is read or written to.</p>
</li>
</ul>
<p>For details on how to set and control breakpoints, see <a href="#Bookmark1350">Using Breakpoints</a>.</p>
<p>A more complicated way to execute until a specified state is reached is to use a <i>conditional breakpoint</i>.  This kind of breakpoint is set at a certain address, but is only triggered if a specified condition holds.  For details, see <a href="#Bookmark1358">Setting a Conditional Breakpoint</a>. </p>
<h3><a id="breakpoints_and_pseudo_registers"></a><a id="BREAKPOINTS_AND_PSEUDO_REGISTERS"></a>Breakpoints and Pseudo-Registers</h3>
<p>In specifying the desired state, it is often helpful to use <i>automatic pseudo-registers</i>.  These are variables controlled by the debugger which allow you to reference a variety of values related to the target state.</p>
<p>For example, the following breakpoint uses the <b>$thread</b> pseudo-register, which is always equal to the value of the current thread.  It resolves to the value of the current thread when it is used in a command.  By using <b>$thread</b> as the argument of the <b>/t</b> parameter of the <a href="#Bookmark1980"><b>bp (Set Breakpoint)</b></a> command, you can create a breakpoint that will be triggered every time that <b>NtOpenFile</b> is called by the thread which was active at the time you issued the <b>bp</b> command:</p>
<div class="code"><span codelanguage=""><table>
<tr>
<th></th>
</tr>
<tr>
<td>
<pre>kd&gt; bp /t @$thread nt!ntopenfile</pre>
</td>
</tr>
</table></span></div>
<p>This breakpoint will not be triggered when any other thread calls <b>NtOpenFile</b>.</p>
<p>For a list of automatic pseudo-registers, see <a href="#Bookmark1925">Pseudo-Register Syntax</a>.</p>
<h3><a id="using_a_script_file_to_control_execution"></a><a id="USING_A_SCRIPT_FILE_TO_CONTROL_EXECUTION"></a>Using a Script File to Control Execution</h3>
<p>Another way to execute until a specified state is reached is to create a script file that calls itself recursively, testing the desired state in each iteration.</p>
<p>Typically, this script file will contain the <a href="#Bookmark1947"><b>.if</b></a> and <a href="#Bookmark1943"><b>.else</b></a> tokens.  You can use a command such as <a href="#Bookmark2033"><b>t (Trace)</b></a> to execute a single step, and then test the condition in question.</p>
<p>For example, if you wish to execute until the <b>eax</b> register contains the value 0x1234, you can create a script file called <i>eaxstep</i> that contains the following line:</p>
<div class="code"><span codelanguage=""><table>
<tr>
<th></th>
</tr>
<tr>
<td>
<pre>.if (@eax == 1234) { .echo 1234 } .else { t "$&lt;eaxstep" }
</pre>
</td>
</tr>
</table></span></div>
<p>Then issue the following command from the Debugger Command window:</p>
<div class="code"><span codelanguage=""><table>
<tr>
<th></th>
</tr>
<tr>
<td>
<pre>t "$&lt;eaxstep"</pre>
</td>
</tr>
</table></span></div>
<p>This <b>t</b> command will execute a single step, and then execute the quoted command.  This command happens to be <a href="#Bookmark1953"><b>$&lt; (Run Script File)</b></a>, which runs the <i>eaxstep</i> file.  The script file tests the value of <b>eax</b>, runs the <b>t</b> command, and then calls itself recursively.  This continues until the <b>eax</b> register equals 0x1234, at which point the <a href="#Bookmark2087"><b>.echo (Echo Comment)</b></a> command prints a message to the Debugger Command window, and execution stops.</p>
<p>For details on script files, see <a href="#Bookmark1269">Using Script Files</a> and <a href="#Bookmark1270">Using Debugger Command Programs</a>. </p></div></div>	
	<div id="winchm_template_footer">Copyright &copy; 2019. All rights 
reserved. (To change the copyright info, just edit it in template.)</div>
</div>

</body>
</html>
